from mcp.server.fastmcp import FastMCP
import sys
import logging

# Fix UTF-8 encoding for Windows console
if sys.platform == 'win32':
    import io
    sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

# 创建MCP服务器实例
mcp = FastMCP("XiaozhiMCP")

# 确保这个变量可以被其他模块导入
__all__ = ['mcp']

def main():
    """启动MCP服务器"""
    # 导入工具，确保在服务器启动前注册所有工具
    from services.amap_service import weather, route_planner, poi_search, forecast
    from services.a12306_mcp import get_station_code, query_tickets, get_current_date
    from services.zhipu_web_search_sse import zhipu_web_search_sse_tool
    
    # 启动MCP服务器，打开调试模式
    mcp.run(transport="stdio")

if __name__ == "__main__":
    main()